/*
 * BNodeAddress.java
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.core.basenet;

public class BNodeAddress extends BNode {

  private BLink link_;
  private int addr_;
  private BNode nearEndNode_,  farEndNode_;
  private BLinkStreet nearEndLink_,  farEndLink_;

  public BNodeAddress(BLinkStreet link, int addr) {
    super(0, link.getScenario(), 0, 0);
    link_ = link;
    addr_ = addr;
    int rMin = Math.min(link.getFAddrR(), link.getTAddrR());
    int rMax = Math.max(link.getFAddrR(), link.getTAddrR());
    int lMin = Math.min(link.getFAddrL(), link.getTAddrL());
    int lMax = Math.max(link.getFAddrL(), link.getTAddrL());
    double addrD = (double) addr, xd, yd, factor = 0;
    if (rMin % 2 == rMax % 2 && rMin % 2 == addr % 2) {
      factor = (double) (addrD - rMin) / (rMax - rMin);
    } else if (lMin % 2 == lMax % 2 && lMin % 2 == addr % 2) {
      factor = (double) (addrD - lMin) / (lMax - lMin);
    } else {
      System.err.println("Error in CNodeAddress constructor: invalid address range");
      return;
    }
    //double nearLen = (double) Math.min(factor, 1-factor)*link.getLengthFeet();
    if (link.getFAddrR() < link.getTAddrR()) {
      xd = link.getX1() + factor * (link.getX2() - link.getX1());
      yd = link.getY1() + factor * (link.getY2() - link.getY1());
      if (factor <= 0.5) {
        nearEndNode_ = link.getFNode();
        farEndNode_ = link.getTNode();
      } else {
        nearEndNode_ = link.getTNode();
        farEndNode_ = link.getFNode();
      }
    } else {
      xd = link.getX2() + factor * (link.getX1() - link.getX2());
      yd = link.getY2() + factor * (link.getY1() - link.getY2());
      if (factor <= 0.5) {
        nearEndNode_ = link.getTNode();
        farEndNode_ = link.getFNode();
      } else {
        nearEndNode_ = link.getFNode();
        farEndNode_ = link.getTNode();
      }
    //nearEndNode_ = (factor <= 0.5) ? link.getTNode() : link.getFNode();	
    }
    //nearEndLink_ = new CLinkStreet(0, this, nearEndNode_, link.getDisplayName(), (int) nearLen);
    //nearEndLink_.setEdge(link.getEdge());
    nearEndLink_ = new BLinkStreetPartial(link, this, nearEndNode_, factor);
    farEndLink_ = new BLinkStreetPartial(link, this, farEndNode_, 1 - factor);
    x_ = (int) xd;
    y_ = (int) yd;
  //super(0,x,y);
  }

  @Override
  public boolean isAddressNode() {
    return true;
  }

  public BLink getLink() {
    return link_;
  }

  public BNode getNearEndNode() {
    return nearEndNode_;
  }

  public BLink getNearEndLink() {
    return nearEndLink_;
  }

  public BNode getFarEndNode() {
    return farEndNode_;
  }

  public BLink getFarEndLink() {
    return farEndLink_;
  }

  public String getAddressStr() {
    return addr_ + " " + link_.getDisplayName();
  }

}



























